const {
  app,
  BrowserWindow,
  Menu,
  ipcMain,
  ipcRenderer,
  webContents,
  Tray,
  screen,
} = require("electron");
const env = process.env.NODE_ENV;

const Store = require("electron-store");
const store = new Store();
const path = require("path");

const icons = `${path.join(__dirname, "../src/assets/images/logo/logo2.ico")}`;

const nameList = [];
let mainWindow = null;
let winMap = new Map();
let windowArr = [];
let windowArr2 = [];
let windowArrIndex = 0;
let windowArrIndex2 = 0;
let mainWin;
exports.createNewWindow = async (param) => {
  if (param.type == 0) {
    mainWin = param.mainWin;
  }
  function getWindows() {
    // 预加载三个窗口
    let temp = [];
    for (let i = 0; i < 3; i++) {
      temp.push(
        new BrowserWindow({
          width: 1200,
          height: 720,
          minHeight: 700,
          minWidth: 1120,
          frame: false,
          // parent: BrowserWindow.getFocusedWindow(),
          parent: mainWin,
          // parentId: winId,
          modal: true,
          icon: icons,
          transparent: false,
          show: false,
          webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true,
          },
        })
      );
      const loadUrl =
        env == "development"
          ? "http://127.0.0.1:5173/#/loading"
          : path.join(__dirname, "../dist/index.html#loading");
      temp[i].loadURL(loadUrl);
      // temp[i].webContents.openDevTools();
    }
    return temp;
  }
  if (param.type == 0) {
    windowArr = getWindows();
  } else {
    createWindow(param.openUrl);
    winMap.set(param.windowName, mainWindow);
    nameList.push(param.windowName);
    // if (!nameList.includes(param.windowName)) {
    //   createWindow(param.openUrl);
    //   winMap.set(param.windowName, mainWindow);
    //   nameList.push(param.windowName);
    // } else {
    //   winMap.get(param.windowName).loadURL(param.openUrl);
    //   winMap.get(param.windowName).show();
    // }
  }
  function createWindow(url) {
    if (windowArrIndex < 3) {
      mainWindow = windowArr[windowArrIndex];
      mainWindow.show();
      windowArrIndex++;
      if (windowArrIndex == 3) {
        windowArrIndex2 = 0;
        windowArr2 = getWindows();
      }
    } else {
      mainWindow = windowArr2[windowArrIndex2];
      mainWindow.show();
      windowArrIndex2++;
      if (windowArrIndex2 == 3) {
        windowArrIndex = 0;
        windowArr = getWindows();
      }
    }
    mainWindow.webContents.send("get-routes", {
      url: url,
      params: param.params == "" ? "" : JSON.parse(param.params),
    });

    // if (windowArrIndex == 0) {
    //   mainWindow.loadURL(url);
    // }

    // mainWindow.on("ready-to-show", (e) => {
    //   if (windowArr.length == 0 && windowArrIndex == 0) {
    //     windowArr = getWindows();
    //   }
    // });

    mainWindow.center();

    mainWindow.setSize(param.width, param.height);
    let position = mainWindow.getPosition();
    console.log(position);
    let screenW = screen.getPrimaryDisplay().workAreaSize.width;

    let num = Math.floor(Math.random() * 20); // 窗口位置随机
    mainWindow.setPosition(
      (screenW - param.width) / 2 + num,
      position[1] - num
    );

    if (env == "development") {
      mainWindow.webContents.openDevTools();
    }

    store.set(param.windowName, mainWindow.webContents.id);
    winMap.set(param.windowName, mainWindow);
    mainWindow.on("close", (e) => {
      winMap.delete(param.windowName);
      nameList.splice(nameList.indexOf(param.windowName), 1);
    });

    mainWindow.on("resize", (e) => {
      try {
        if (!mainWindow.isMaximized()) {
          mainWindow.webContents.send("restoreMaximize", "restore");
        } else {
          mainWindow.webContents.send("restoreMaximize", "maximize");
        }
      } catch (error) {}
    });

    homeWinow = winMap.get("home");
  }
};

exports.closeWindow = (win) => {
  for (let key of winMap.keys()) {
    try {
      if (winMap.get(key) == win) {
        winMap.get(key).close();
        winMap.delete(key);
      }
    } catch (error) {
      console.log(error);
    }
  }
};
